package com.piece.mall.controller;

import com.piece.core.framework.annotation.permission.SubmitPermission;
import com.piece.core.framework.support.response.AjaxResponse;
import com.piece.core.framework.util.net.IpUtil;
import com.piece.core.framework.util.time.DateUtil;
import com.piece.core.log.annotation.OperationType;
import com.piece.core.log.enums.AuditLogType;
import com.piece.core.pay.dto.PayOrderUnifiedReqDTO;
import com.piece.core.pay.dto.PayRefundUnifiedReqDTO;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.mall.service.OmsOrderService;
import com.piece.mall.service.PaymentService;
import com.piece.mall.service.PmsSkuInfoService;
import com.piece.mall.service.PmsSpuInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

@Slf4j
@RestController
@Api(tags = {"支付操作管理"})
@RequestMapping("/api/mall/payment")
public class PaymentController extends FrameWorkController {

    @Resource
    private PaymentService paymentService;

    @Resource
    private PmsSpuInfoService pmsSpuInfoService;

    @Resource
    private PmsSkuInfoService pmsSkuInfoService;

    @Resource
    private OmsOrderService omsOrderService;

    @SubmitPermission
    @PostMapping("/payoff/{channel}/{code}")
    @ApiOperation(value = "付款")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "下单请求数据", dataTypeClass = PayOrderUnifiedReqDTO.class, type = "body"),
            @ApiImplicitParam(name="channel", value = "付款渠道", type = "path"),
            @ApiImplicitParam(name="code", value = "渠道类别", type = "path")
    })
    @OperationType(AuditLogType.OTHER)
    public AjaxResponse payoff(@PathVariable String channel, @PathVariable String code, @RequestBody PayOrderUnifiedReqDTO payOrder) {
        payOrder.setUserIp(IpUtil.getIp(getRequest(), true));
        payOrder.setExpireTime(DateUtil.addHour(2));
        try {
            return paymentService.payoff(channel, code, payOrder);
        } catch (Exception e) {
            return AjaxResponse.error(e.getMessage());
        }
    }

    @SubmitPermission
    @PostMapping("/refund/{channel}/{code}")
    @ApiOperation(value = "退款")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "退款请求数据", dataTypeClass = PayRefundUnifiedReqDTO.class, type = "body"),
            @ApiImplicitParam(name="channel", value = "付款渠道", type = "path"),
            @ApiImplicitParam(name="code", value = "渠道类别", type = "path")
    })
    @OperationType(AuditLogType.OTHER)
    public AjaxResponse refund(@PathVariable String channel, @PathVariable String code, @RequestBody PayRefundUnifiedReqDTO refundOrder) {
        refundOrder.setUserIp(IpUtil.getIp(getRequest(), true));
        try {
            return paymentService.refund(channel, code, refundOrder);
        } catch (Exception e) {
            return AjaxResponse.error(e.getMessage());
        }
    }
}
